package io.edap.protobuf.compiler.maven;

import io.edap.protobuf.builder.JavaBuildOption;
import io.edap.protobuf.codegen.JavaGenerator;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.nio.file.Path;

import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE_PLUS_RUNTIME;

/**
 * Java generator MOJO.
 *
 * @author veione
 */
@Mojo(name = "java",
        threadSafe = true,
        configurator = "include-project-dependencies",
        requiresDependencyResolution = COMPILE_PLUS_RUNTIME)
public class JavaGeneratorMojo extends AbstractGeneratorMojo {

    @Parameter(defaultValue = "src/main/java")
    private File target;
    @Parameter(defaultValue = "")
    private String dtoPrefix;
    @Parameter(defaultValue = "true")
    private boolean enableChain;
    @Parameter(defaultValue = "false")
    private boolean enableBoxed;
    @Parameter(defaultValue = "false")
    private boolean enableDefaultValue;
    @Parameter(defaultValue = "false")
    private boolean enableEdapRpc;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        super.execute();

        JavaBuildOption buildOption = new JavaBuildOption();

        final Path sourcePath = getSourcePath();
        buildOption.setChainOper(enableChain);
        buildOption.setDtoPrefix(dtoPrefix);
        buildOption.setUseBoxed(enableBoxed);
        buildOption.setHasDefaultValue(enableDefaultValue);
        buildOption.setEdapRpc(enableEdapRpc);

        //获取需要构建的proto文件列表
        File proto = sourcePath.toFile();
        JavaGenerator.generate(proto, target, buildOption);
    }
}

